﻿//==============================================================================
// File: TransientProperty.cs
// Created: 2010-05-10
// Author: Piotr Włodek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with WpfRcp. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.Diagnostics;

namespace WpfRcp.PropertyModel.Core
{
    /// <summary>
    /// Represents an implementation of <see cref="IProperty"/> interface
    /// specific to transient properties.
    /// </summary>
    [DebuggerDisplay("Name = {MetaProperty.Name}, IsValid = {IsValid}")]
    [Serializable]
    public class TransientProperty : IProperty
    {
        private readonly MetaProperty m_MetaProperty;
        private readonly IPropertyContainer m_Original;

        public TransientProperty(MetaProperty property, IPropertyContainer original)
        {
            if (property == null)
                throw new ArgumentNullException("property");

            if (original == null)
                throw new ArgumentNullException("original");

            m_MetaProperty = property;
            m_Original = original;
        }

        /// <summary>
        /// Gets a <see cref="IProperty.MetaProperty"/> instance identifying this value.
        /// </summary>
        public MetaProperty MetaProperty
        {
            get
            {
                ThrowIfDisposed();
                return m_MetaProperty;
            }
        }

        /// <summary>
        /// Gets or sets a value.
        /// </summary>
        public virtual object Value
        {
            get
            {
                ThrowIfDisposed();
                return MetaProperty.Callback(m_Original);
            }
            set { throw new NotSupportedException(); }
        }

        #region IDisposable Implementation

        [NonSerialized]
        private bool m_Disposed;

        /// <summary>
        /// Returns true if object has been disposed.
        /// </summary>
        protected bool Disposed
        {
            get
            {
                lock (this)
                {
                    return m_Disposed;
                }
            }
        }

        /// <summary>
        /// Performs cleaning. Always call base class's <see cref="Dispose(bool)"/> method.
        /// </summary>
        /// <param name="disposing">True is this method is called via <see cref="Dispose"/> method.
        /// False indicates that the method has been called from destructor.</param>
        /// <remarks>
        /// Generally, you should dispose both managed and unmanaged resources when <paramref name="disposing"/>
        /// is true, and only unmanaged resources when <paramref name="disposing"/> is false.
        /// </remarks>
        protected virtual void Dispose(bool disposing)
        {
        }

        public void Dispose()
        {
            lock (this)
            {
                if (m_Disposed == false)
                {
                    Dispose(true);
                    m_Disposed = true;

                    GC.SuppressFinalize(this);
                }
            }
        }

        /// <summary>
        /// Destructor will run only if <see cref="Dispose"/> wasn't called.
        /// Do not provide descructors in derived types. Use <see cref="Dispose(bool)"/> instead.
        /// </summary>
        ~TransientProperty()
        {
            Dispose(false);
        }

        [DebuggerStepThrough]
        protected void ThrowIfDisposed()
        {
            if (Disposed)
                throw new ObjectDisposedException(GetType().Name);
        }

        #endregion
    }
}